﻿/*
 * My Timer - cdShutdown.cs
 * 
 * Copyright (C) 2009 Ahmad Hanis Azmi
 * 
 * This file is part of My Timer
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

using System;
using System.Management;
using System.Windows.Forms;

namespace My_Timer.Shutdown
{
    public static class Shutdown
    {   
        public static void f_Shutdown(string Flags)
        {
            SaveSetting.SaveAllSetting();

            ManagementBaseObject mboShutdown = null;
            ManagementClass mcWin32 = new ManagementClass("Win32_OperatingSystem");
            mcWin32.Get();

            // You can't shutdown without security privileges
            mcWin32.Scope.Options.EnablePrivileges = true;
            ManagementBaseObject mboShutdownParams = mcWin32.GetMethodParameters("Win32Shutdown");

            // Flag 1 means we want to shut down the system. Use "2" to reboot.
            //mboShutdownParams["Flags"] = "1";
            mboShutdownParams["Flags"] = Flags;
            mboShutdownParams["Reserved"] = "0";
            foreach (ManagementObject manObj in mcWin32.GetInstances())
            {
                mboShutdown = manObj.InvokeMethod("Win32Shutdown", mboShutdownParams, null);
            }

            Program.ExitApplication();
        }
        public static void tf_Shutdown(string Flags)
        {
            MessageBox.Show("Shutdown command sent! Flag: " + Flags);
        }
    }
 }